#include "common.h"
PUBLIC S4B MMPageDistribute(PMMPARAMETER pparameter)
{
   if(pparameter->pagenumber==0)
   	return ARTIST_ERROR_OK;
   if(pparameter->pagenumber>pblock->time) /*I must say,the first block is just a flag,the time show the remain number of the free page.*/
   {
      pparameter->blockpointer=PTR_INVALID;
	  pparameter->pagenumber=0;
      return ARTIST_ERROR_PAGENOTENOUGH;
   }
   PPAGEBLOCK block=pblock,pgetblock=pblock->nextblock;
   U4B j;
   for(j=0;j<pparameter->pagenumber;j++) 
   	block=block->nextblock;
   
   pblock->nextblock=block->nextblock;
   block->nextblock->prevblock=pblock;
   block->nextblock=(PPAGEBLOCK)PTR_INVALID;
   pblock->time-=pparameter->pagenumber;  
   pparameter->blockpointer=pgetblock;
   return ARTIST_ERROR_OK;
}


PUBLIC S4B MMPageRestore(PMMPARAMETER pparameter)
{
           PPAGEBLOCK block=pblock;
           PPAGEBLOCK preturnblock=pparameter->blockpointer;
           PPAGEBLOCK p=preturnblock;
           U4B i;
           for(i=1;preturnblock->nextblock!=(PPAGEBLOCK)PTR_INVALID;preturnblock=preturnblock->nextblock)
           {i++;}
           preturnblock->nextblock=block;
           p->prevblock=block->prevblock;
           block->prevblock->nextblock=p;
           block->prevblock=preturnblock;
           block->time+=i;
           return ARTIST_ERROR_OK;
}


PUBLIC S4B MMLockPage(PMMPARAMETER pparameter)
{
    PPAGEBLOCK p=pparameter->blockpointer;
	for(;p!=PTR_INVALID;p=p->nextblock)
	{
	    p->attribute|=PAGEFIXED;
	}
    return ARTIST_ERROR_OK;
}

PUBLIC S4B MMUnlockPage(PMMPARAMETER pparameter)
{
   PPAGEBLOCK p=pparameter->blockpointer;
	for(;p!=PTR_INVALID;p=p->nextblock)
	{
	    p->attribute&=(!PAGEFIXED);
	}
   return ARTIST_ERROR_OK;
}

PUBLIC S4B MMApplyForPage(PMMPARAMETER pparameter)
{
   	MESSAGE message;
	message.destination=MMSERVER;
	message.type=MMSERVER_PAGEDISTRIBUTION;
	message.value0=(U4B)(pparameter);
	SendMessage(&message);
	SIGNAL signal;
	signal.dt=0;
	signal.signal=MMSERVER_PAGEDISTRIBUTION;
	signal.sourcepid=MMSERVER;
	WaitSignal(&signal);	
	return signal.returnvalue;
}

PUBLIC S4B ApplyForLocking(PMMPARAMETER pparameter)
{

   MESSAGE message;
   message.destination=MMSERVER;
   message.type=MMSERVER_LOCKPAGE;
   message.value0=(U4B)(pparameter);
   SendMessage(&message);
   SIGNAL signal;
   signal.dt=0;
   signal.signal=MMSERVER_LOCKPAGE;
   signal.sourcepid=MMSERVER;
   WaitSignal(&signal);	
   return signal.returnvalue;
}


